//index.js
/*
 * 以当前月份为中心，获取5个月的数据
 */
var nowDate = new Date();
var year = nowDate.getFullYear();
var month = nowDate.getMonth();

var createDate = (year, month)=> {
  var startYear = year,
      startMonth = month - 2,        //以传入月份为中心，所以需要-2获取开始月份
      monthStart = '',
      numDate = '',
      dateArray = [];
  (startMonth < 0)&&(startMonth = startMonth + 12)&&(startYear = startYear - 1); 
  // monthStart = getMonthStart(startYear, startMonth);
  // dateArray.push({'year': startYear, 'month': startMonth, 'monthStart': monthStart});
  for(var i = 0; i < 5; i++){
    var nowYear = startYear,
        nowMonth = startMonth;
    if( (nowMonth+i) < 12){
      nowMonth = nowMonth + i;
    } else {
      nowMonth = nowMonth + i - 12;
      nowYear++;
    }
    numDate = getNumDate(nowYear, nowMonth)
    monthStart = getMonthStart(nowYear, nowMonth);
    dateArray.push({'year': nowYear, 'month': nowMonth, 'numDate' : numDate,  'monthStart': monthStart});
  }
  return dateArray;
};

var getMonthStart = (year, month)=> {
  var newDate = new Date(year, month);
  return newDate.getDay();
}
//返回该月天数
var getNumDate = (year, month)=>{             
  //定义当月的天数；
  var days ;
  //当月份为二月时，根据闰年还是非闰年判断天数
  if(month == 2){
        days= year % 4 == 0 ? 29 : 28;        
    }
    else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
        //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
        days= 31;
    }
    else{
        //其他月份，天数为：30.
        days= 30;
    }
    return days;
}
Page({
  data: {
    test: [{"2016/10/12":[{"23":"城市探索"},{"41":"寻迹佛山"}]},{"2016/11/24":[{"13":"城市探索"},{"24":"寻迹佛山"}]},{"2016/12/1":[{"23":"城市探索"},{"41":"寻迹佛山"}]}],
    week: ["日", "一", "二", "三", "四", "五", "六"],
    dateArray: createDate(year, month),
    date: year + '年'+(month+1)+'月'
  },
  changeSwiper: function(e){
    var self = this;
    self.setData({
      date: self.data.dateArray[e.detail.current].year+'年' + (self.data.dateArray[e.detail.current].month+1) + '月'
    })
  },
  onShow: function(){
    console.log('这是直播页面');
  }
})
